package com.dawn.modules.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dawn.modules.chat.entity.Notebook;

import com.dawn.modules.chat.mapper.NotebookMapper;
import com.dawn.modules.chat.service.NotebookService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NotebookServiceImpl extends ServiceImpl<NotebookMapper, Notebook> implements NotebookService {

    @Override
    public List<Notebook> getNoteBook(Notebook notebook) {
        LambdaQueryWrapper<Notebook> queryWrapper = new LambdaQueryWrapper<>();
        if (notebook.getUserId() != null) {
            queryWrapper.eq(Notebook::getUserId, notebook.getUserId());
        }
        if (StringUtils.isNotBlank(notebook.getNotebookName())) {
            queryWrapper.eq(Notebook::getNotebookName, notebook.getNotebookName());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean addNoteBook(Integer userId, String noteBookName) {
        Notebook notebook = Notebook.builder().
                userId(userId)
                .notebookName(noteBookName)
                .build();
        List<Notebook> notebooks = getNoteBook(notebook);
        // 出现重名
        if (notebooks.stream().anyMatch(noteBook -> noteBook.getNotebookName().equals(noteBookName))) {
            return false;
        }
        Notebook noteBook1 = new Notebook();
        noteBook1.setUserId(userId);
        noteBook1.setNotebookName(noteBookName);
        return save(noteBook1);
    }

    @Override
    public Boolean deleteNoteBook(Integer userId, String noteBookName) {
        LambdaQueryWrapper<Notebook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notebook::getUserId, userId);
        queryWrapper.eq(Notebook::getNotebookName, noteBookName);

        // todo 删除笔记簿还需删除其中的所有笔记
        return remove(queryWrapper);
    }

    @Override
    public Notebook getNoteBookByName(Integer userId, String noteBookName) {
        LambdaQueryWrapper<Notebook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notebook::getUserId, userId);
        queryWrapper.eq(Notebook::getNotebookName, noteBookName);
        return getOne(queryWrapper);
    }

    @Override
    public Boolean addNoteToNoteBook(Integer userId, String noteBookName, Integer noteId) {
        // todo 获取userID

        Notebook noteBook = getNoteBookByName(userId, noteBookName);
        if (noteBook == null) {
            return false;
        }
        String noteIds = noteBook.getNoteIds();
        if (StringUtils.isBlank(noteIds) || noteIds.isEmpty()) {
            // 防止出现幻觉
            // 当当 noteIds 为空时，noteIds += noteId 会变成 "null" + noteId
            noteIds = String.valueOf(noteId);
        } else {
            noteIds = noteIds + "," + noteId;
        }
        return update(new LambdaUpdateWrapper<Notebook>()
                .eq(Notebook::getUserId, userId)
                .eq(Notebook::getNotebookName, noteBookName)
                .set(Notebook::getNoteIds, noteIds));
    }

}
